package com.example.demo.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.demo.constant.TaskConstant;
import com.example.demo.controller.bean.Result;
import com.example.demo.dto.CouponMonitorDTO;
import com.example.demo.dto.CouponsDetailDTO;
import com.example.demo.mapper.entity.BadgeView;
import com.example.demo.mapper.entity.CouponsView;
import com.example.demo.mapper.entity.StudentCouponsView;
import com.example.demo.service.ClothingManageService;
import com.example.demo.service.CouponMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("couponMonitor")
@Api(value = "优惠券监控接口", tags = {"优惠券监控接口"})
@RestController
@Slf4j
public class CouponMonitorController {
    @Autowired
    private CouponMonitorService service;

    @ResponseBody
    @ApiOperation("收集数据接口")
    @RequestMapping(value = "getCouponsData/{totalNum}", method = {RequestMethod.POST})
    public SaResult getCouponsData(@PathVariable("totalNum")int totalNum) {
        if (StpUtil.getLoginIdDefaultNull() != null) {
            // 获取当前会话账号id, 如果未登录，则返回 null
            int userId = StpUtil.getLoginIdAsInt();
            List<CouponMonitorDTO> couponsDataList = service.getCouponsData(userId, totalNum);
            List<CouponMonitorDTO> sortedList = couponsDataList.stream()
                .sorted(Comparator.comparing(CouponMonitorDTO::getWeightedAverage).thenComparing(CouponMonitorDTO::getName))
                .collect(Collectors.toList());
            return SaResult.data(sortedList);
        } else {
            return SaResult.error("未登录");
        }
    }

    @ResponseBody
    @ApiOperation("收集优惠券数据接口")
    @RequestMapping(value = "getCouponsDateData", method = {RequestMethod.POST})
    public SaResult getCouponsDateData() {
        //Stu
        List<StudentCouponsView> couponsDataList = service.getCouponsDateData();
        for (StudentCouponsView logs : couponsDataList) {

        }
        return SaResult.data(2);
    }

    @ResponseBody
    @ApiOperation("优惠券报告数据接口")
    @RequestMapping(value = "getCouponsReportData/{totalNum}", method = {RequestMethod.POST})
    public SaResult getCouponsReportData(@PathVariable("totalNum")int totalNum) {
         if (StpUtil.getLoginIdDefaultNull() != null) {
            // 获取当前会话账号id, 如果未登录，则返回 null
           int userId = StpUtil.getLoginIdAsInt();
            CouponMonitorDTO couponsReportData = service.getCouponsReportData(userId, totalNum);
            return SaResult.data(couponsReportData);
        } else {
            return SaResult.error("未登录");
        }
    }

    @ResponseBody
    @ApiOperation("优惠券统计数据接口")
    @RequestMapping(value = "getStatisticData", method = {RequestMethod.POST})
    public SaResult getStatisticData() {
        if (StpUtil.getLoginIdDefaultNull() != null) {
            // 获取当前会话账号id, 如果未登录，则返回 null
            int userId = StpUtil.getLoginIdAsInt();
            CouponMonitorDTO couponsStatisticData = service.getStatisticData(userId);
            return SaResult.data(couponsStatisticData);
        } else {
            return SaResult.error("未登录");
        }
    }

}
